package net.csdn.business.discuss.api.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.mapper.DiscussCommentMapper;
import net.csdn.business.discuss.api.mapper.DiscussLikeRecordMapper;
import net.csdn.business.discuss.api.mapper.DiscussMapper;
import net.csdn.business.discuss.api.model.dto.DiscussLikeDTO;
import net.csdn.business.discuss.api.model.entity.DiscussLikeRecord;
import net.csdn.business.discuss.api.model.query.BatchLikeRecordQuery;
import net.csdn.business.discuss.api.model.query.DiscussLikeRecordQuery;
import net.csdn.business.discuss.api.model.vo.DiscussLikeRecordVO;
import net.csdn.business.discuss.api.service.IDiscussLikeService;
import net.csdn.business.discuss.common.constant.RedisConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 讨论点赞记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DiscussLikeServiceImpl extends ServiceImpl<DiscussLikeRecordMapper, DiscussLikeRecord> implements IDiscussLikeService
{
    @Autowired
    private CacheManager cacheManager;

    @Autowired
    private DiscussMapper discussMapper;

    @Autowired
    private DiscussCommentMapper discussCommentMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer like(DiscussLikeDTO params) {
        Integer result=0;
        Integer targetType=params.getTargetType();
        String targetId=params.getTargetId();
        String currUserId=params.getUserId();
        DiscussLikeRecordVO record=baseMapper.selectDiscussVoteRecord(params);
        if(record==null){
            //没有点赞记录，则插入
            DiscussLikeRecord likeRecord=new DiscussLikeRecord();
            BeanUtils.copyProperties(params,likeRecord);
            String id= IdUtil.fastSimpleUUID();
            likeRecord.setObjectId(id);
            result=baseMapper.insert(likeRecord);
            if(result>0){
                switch (targetType){
                    case 1:
                        //修改讨论的点赞数量
                        discussMapper.updateLikeTotal(targetId,1);
                        //清除讨论列表缓存
                        String sourceId=discussMapper.selectSourceId(targetId);
                        StringBuilder cacheName=null;
                        try{
                            cacheName = new StringBuilder(RedisConstants.DISCUSS_LIST_PAGE);
                            cacheName.append(sourceId);
                            Cache cache = cacheManager.getCache(cacheName.toString());
                            cache.clear();
                            StringBuilder myDiscussCacheName = new StringBuilder(RedisConstants.DISCUSS_MY_LIST_PAGE);
                            myDiscussCacheName.append(currUserId);
                            StringBuilder myJoinDiscussCacheName = new StringBuilder(RedisConstants.DISCUSS_JOIN_LIST_PAGE);
                            myJoinDiscussCacheName.append(currUserId);
                            Cache myDiscussCache = cacheManager.getCache(myDiscussCacheName.toString());
                            myDiscussCache.clear();
                            Cache myJoinDiscussCache = cacheManager.getCache(myDiscussCacheName.toString());
                            myJoinDiscussCache.clear();
                        }catch (Exception e){
                            log.info("cache evict cache name:{} error: {} !",cacheName.toString(),e.getMessage());
                        }
                        break;
                    case 2:
                    case 3:
                        discussCommentMapper.updateLikeTotal(targetId,1);
                        break;
                }
            }
        }
        return result;
    }

    @Override
    public Integer unLike(DiscussLikeDTO params) {
        Integer targetType=params.getTargetType();
        String targetId=params.getTargetId();
        String currUserId=params.getUserId();
        //有点赞记录，则删除
        int res=baseMapper.deleteDiscussLikeRecord(params);
        if(res>0){
            switch (targetType){
                case 1:
                    //修改讨论的点赞数量
                    discussMapper.updateLikeTotal(targetId,-1);
                    //清除讨论列表缓存
                    String sourceId=discussMapper.selectSourceId(targetId);
                    if(StringUtils.isNotBlank(sourceId)){
                        StringBuilder cacheName=null;
                        try{
                            cacheName = new StringBuilder(RedisConstants.DISCUSS_LIST_PAGE);
                            cacheName.append(sourceId);
                            Cache cache = cacheManager.getCache(cacheName.toString());
                            cache.clear();
                            StringBuilder myDiscussCacheName = new StringBuilder(RedisConstants.DISCUSS_MY_LIST_PAGE);
                            myDiscussCacheName.append(currUserId);
                            StringBuilder myJoinDiscussCacheName = new StringBuilder(RedisConstants.DISCUSS_JOIN_LIST_PAGE);
                            myJoinDiscussCacheName.append(currUserId);
                            Cache myDiscussCache = cacheManager.getCache(myDiscussCacheName.toString());
                            myDiscussCache.clear();
                            Cache myJoinDiscussCache = cacheManager.getCache(myDiscussCacheName.toString());
                            myJoinDiscussCache.clear();
                        }catch (Exception e){
                            log.info("cache evict cache name:{} error: {} !",cacheName.toString(),e.getMessage());
                        }
                    }
                    break;
                case 2:
                case 3:
                    discussCommentMapper.updateLikeTotal(targetId,-1);
                    break;
            }
        }
        return res;
    }

    @Override
    public List<String> selectLikeRecordBatch(BatchLikeRecordQuery params) {
        return baseMapper.selectLikeRecordBatch(params);
    }

    @Override
    public Boolean selectLikeRecord(DiscussLikeRecordQuery query) {
        DiscussLikeRecordVO discussLikeRecordVO=baseMapper.selectLikeRecord(query);
        if(discussLikeRecordVO!=null){
           return true;
        }
        return false;
    }
}
